package org.ala.tcp_feign_client.handler;

import org.ala.tcp_feign_client.handler.biz.BizRespByteMessageAction;
import org.ala.tcp_feign_client.handler.close.CloseByteMessageAction;
import org.ala.tcp_feign_client.handler.heart.HeartACKByteMessageAction;
import org.ala.tcp_feign_client.handler.login.LoginACKByteMessageAction;
import org.ala.tcp_feign_common.config.AttrbuteKeyNamespace;
import org.ala.tcp_feign_common.handler.ByteMessageReadHandler;
import org.ala.tcp_feign_common.handler.MessageDecodeHandler;
import org.ala.tcp_feign_common.handler.MessageEncodeHandler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.util.AttributeKey;


/**
 * 客户端channel初始化
 *
 * @author 骆毅(ala)
 * @date 2024年4月20日
 */
public class TcpFeignClientInitializeHandler extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		//	记录3次握手成功时间戳
		ch.attr(AttributeKey.valueOf(AttrbuteKeyNamespace.CONNECT_TIMESTAMP)).set(System.currentTimeMillis());
		
		//	物理解码 -> 消息action -> 物理编码
		ch.pipeline().addLast(new MessageDecodeHandler());
		ch.pipeline().addLast(actionHandler());
		ch.pipeline().addLast(new MessageEncodeHandler());
	}
	/**
	 * action
	 */
	protected ByteMessageReadHandler actionHandler() {
		ByteMessageReadHandler handler = new ByteMessageReadHandler();
		
		handler.append(new LoginACKByteMessageAction());
		handler.append(new HeartACKByteMessageAction());
		handler.append(new CloseByteMessageAction());
		handler.append(new BizRespByteMessageAction());
		
		return handler;
	}

}
